package btb.net;

import java.io.IOException;

import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;

import btb.engine.ATask;
import btb.engine.CEvent;
import btb.engine.CTaskMessenger;
import btb.net.CNetwork.NChatMessage;
import btb.net.CNetwork.NChatOpResults;
import btb.net.CNetwork.NUpdateChannelList;
import btb.util.CLogger;

public class CChatClientTask extends ATask {
	protected Client client = null;
	protected String name = "Unknown";
	
	protected int networkType = CNetwork.CHAT_CLIENT;
	
	protected Listener chatListener = new Listener()
	{
		public void received(Connection _c, Object _o)
		{
			if(_o instanceof NChatMessage)
			{
				CNetworkEvent netevent = new CNetworkEvent(CChatClientTask.this, CNetworkEvent.CNE_CHAT_MESSAGE, _o);
				CChatClientTask.this.getMailbox().push(CTaskMessenger.TASK_DISPLAY, netevent);
			}
			else if(_o instanceof NChatOpResults)
			{
				CNetworkEvent netevent = new CNetworkEvent(CChatClientTask.this, CNetworkEvent.CNE_CHAT_CHANNEL_OP, _o);
				CChatClientTask.this.getMailbox().push(CTaskMessenger.TASK_DISPLAY, netevent);
			}
		}
	};
	
	public CChatClientTask(ATask _parentTask, int _taskId, CTaskMessenger _mailbox)
	{
		super(_parentTask, _taskId, _mailbox);
	}
	
	public boolean isConnected()
	{
		return this.client != null && this.client.isConnected();
	}
	
	public void setFriendlyName(String _name) {
		this.name = _name;
	}
	
	public boolean setUp()
	{
		boolean success = true;
		
		this.client = new Client();
		this.client.start();
		
		CNetwork.register(this.client);
		
		this.client.addListener(this.chatListener);
		
		try
		{
			this.client.connect(5000, CNetwork.CHAT_SERVER_HOST, CNetwork.CHAT_SERVER_PORT);
		}
		catch (IOException ioe)
		{
			success = false;
			CLogger.cond_warn("chat client", "IOException thrown while trying to connect.");
			ioe.printStackTrace();
		}
		
		return success;
	}
	
	public boolean tearDown()
	{
		CLogger.cond_debug("chat client", "Being torn down.");
		boolean success = true;
		
		if(this.isConnected())
		{
			this.client.stop();
			this.client = null;
		} else
			success = false;
		
		return success;
	}

	@Override
	public void update()
	{
		this.handleMail();
	}

	@Override
	public void handleEvent(CEvent _event)
	{
		CLogger.cond_trace("chat client", "handling events.");
		
		if(CNetworkEvent.class.isAssignableFrom(_event.getClass()))
		{
			CNetworkEvent cne = (CNetworkEvent)_event;
			
			switch(cne.getEventType())
			{
			case CNetworkEvent.CNE_CHAT_MESSAGE:
				NChatMessage msg = (NChatMessage) _event.getEventPayload();
				this.client.sendTCP(msg);
				
				break;
			case CNetworkEvent.CNE_CHAT_CHANNEL_OP:
				NChatOpResults ncor = (NChatOpResults) _event.getEventPayload();
				this.client.sendTCP(ncor);
				break;
			case CNetworkEvent.CNE_CHAT_UPDATE:
				NUpdateChannelList nucl = new NUpdateChannelList();
				this.client.sendTCP(nucl);
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void handleDeath()
	{
		// TODO Auto-generated method stub

	}

}
